package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.autocode.ZlValidationMapper;
import com.my.zl.console.dto.autocode.validation.ReqValidationAddDTO;
import com.my.zl.console.dto.autocode.validation.ReqValidationDelDTO;
import com.my.zl.console.dto.autocode.validation.ReqValidationDetailDTO;
import com.my.zl.console.dto.autocode.validation.ReqValidationEditDTO;
import com.my.zl.console.dto.autocode.validation.ReqValidationQueryDTO;
import com.my.zl.console.dto.autocode.validation.ReqValidationStateDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationAddDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationDelDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationDetailDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationEditDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationQueryDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationStateDTO;
import com.my.zl.console.dto.autocode.validation.ResValidationVO2;
import com.my.zl.console.entity.autocode.ZlValidation;
import com.my.zl.console.po.autocode.validation.ZlValidationVo;
import com.my.zl.console.service.autocode.IValidationService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 校验规则
 * @author ZL
 */
@Service
public class ValidationServiceImpl extends BaseManager implements IValidationService {

    @Autowired
    private ZlValidationMapper validationMapper;

    @Override
    public ResValidationQueryDTO queryValidationList(ReqValidationQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResValidationQueryDTO resDTO = BeanUtil.copyProperties(page, ResValidationQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getValidationTitle())){
            reqInfo.setValidationTitle("%"+reqInfo.getValidationTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlValidationVo> list = validationMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResValidationVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResValidationDetailDTO queryValidationDetail(ReqValidationDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResValidationDetailDTO res = new ResValidationDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlValidation view = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResValidationAddDTO insertValidationInfo(ReqDTO<ReqValidationAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationAddDTO reqData = reqInfo.getReqData();
        ResValidationAddDTO res = new ResValidationAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setValidationId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        //检查主键是否冲突
        if(validationMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        validationMapper.insertSelective(record);
        
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResValidationEditDTO updateValidationInfo(ReqDTO<ReqValidationEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationEditDTO reqData = reqInfo.getReqData();
        ResValidationEditDTO res = new ResValidationEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        validationMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResValidationDelDTO deleteValidationInfo(ReqDTO<ReqValidationDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationDelDTO reqData = reqInfo.getReqData();
        ResValidationDelDTO res = new ResValidationDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        validationMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResValidationStateDTO updateValidationState(ReqDTO<ReqValidationStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqValidationStateDTO reqData = reqInfo.getReqData();
        ResValidationStateDTO res = new ResValidationStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlValidation record = new ZlValidation();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        validationMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("validationId",record.getValidationId());
        ZlValidation keyInfo = validationMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}